﻿#include <libini/string.h>


// constructor
Xanadu::ini::string::string() noexcept
{
}

// destructor
Xanadu::ini::string::~string() noexcept
{
}



// According to the specified mark
std::list<std::string> Xanadu::ini::string::split(const std::string& _String, const std::string& _Mark) noexcept
{
	auto		vIndex = 0U;
	auto 		vStringArray = std::list<std::string>();
	auto		vFind = _String.find(_Mark, vIndex);
	while (vFind != std::string::npos)
	{
		vStringArray.push_back(_String.substr(vIndex, vFind - vIndex));
		vIndex = vFind + _Mark.size();
		vFind = _String.find(_Mark, vIndex);
	};
	if(vIndex < _String.size())
	{
		vStringArray.push_back(_String.substr(vIndex));
	}
	return vStringArray;
}

// Convert to uppercase
std::string Xanadu::ini::string::to_upper(const std::string& _String) noexcept
{
	auto 		vUpper = std::string();
	for(auto vIterator = _String.begin(); vIterator != _String.end(); ++vIterator)
	{
		vUpper.push_back(std::toupper(*vIterator));
	}
	return vUpper;
}

// Convert to lowercase
std::string Xanadu::ini::string::to_lower(const std::string& _String) noexcept
{
	auto 		vUpper = std::string();
	for(auto vIterator = _String.begin(); vIterator != _String.end(); ++vIterator)
	{
		vUpper.push_back(std::tolower(*vIterator));
	}
	return vUpper;
}



// Extract section
std::string Xanadu::ini::string::extract_section(const std::string& _String) noexcept
{
	if(Xanadu::ini::string::is_section(_String))
	{
		return _String.substr(1, _String.size() - 2);
	}
	return std::string();
}



// Check whether it is a wrong line of data
bool Xanadu::ini::string::is_error(const std::string& _String) noexcept
{
	if(Xanadu::ini::string::is_empty(_String))
	{
		return false;
	}
	else if(Xanadu::ini::string::is_explain(_String))
	{
		return false;
	}
	else if(Xanadu::ini::string::is_section(_String))
	{
		return false;
	}
	else if(Xanadu::ini::string::is_key(_String))
	{
		return false;
	}
	else
	{
		return true;
	}
}

// Check whether the line is empty
bool Xanadu::ini::string::is_empty(const std::string& _String) noexcept
{
	return _String.empty();
}

// Check whether the line is explain
bool Xanadu::ini::string::is_explain(const std::string& _String) noexcept
{
	if(_String.empty())
	{
		return false;
	}
	return _String[0] == LIBINI_EXPLAIN_CHAR;
}

// Check whether the line is section
bool Xanadu::ini::string::is_section(const std::string& _String) noexcept
{
	if(_String.size() < 3)
	{
		return false;
	}
	return _String[0] == LIBINI_SECTION_BEGIN && _String[_String.size() - 1] == LIBINI_SECTION_END;
}

// Check whether the line is key
bool Xanadu::ini::string::is_key(const std::string& _String) noexcept
{
	if(_String.size() < 2)
	{
		return false;
	}
	auto		vFind = _String.find(LIBINI_KEY_DIVISION);
	if(vFind == std::string::npos || vFind == 0)
	{
		return false;
	}
	return true;
}
